/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       TestController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/11/9      feiwe                         add
 */


package com.fengwei.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.form.MdmsUser;
import com.fengwei.requestdata.login.LoginBindGoogleAuthData;
import com.fengwei.requestdata.login.LoginData;
import com.fengwei.requestdata.login.LoginGoogleAuthData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.LoginService;
import com.fengwei.util.MdmsConsts;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.util.VerifyException;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @GetMapping(value = "/")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        //移除用户信息
        session.removeAttribute("mdmsUser");
        return "login";
    }

    /***
     * 一次性密码获取
     */
    @PostMapping(path = "/login/getOtp")
    @ResponseBody
    public Map<String, Object> getOtp(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        // 正式处理业务
        Map<String, Object> data = loginService.getOtp(request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    @PostMapping(value = "/login/login")
    @ResponseBody
    public Map<String, Object> login(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        LoginData loginData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<LoginData>() {
        });

        // 校验传入参数
        Verify.verifyData(loginData);

        // 正式处理业务
        Map<String, Object> data = loginService.login(loginData, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    @PostMapping(value = "/login/bindGoogleAuth")
    @ResponseBody
    public Map<String, Object> bindGoogleAuth(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        LoginBindGoogleAuthData loginBindGoogleAuthData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<LoginBindGoogleAuthData>() {
        });

        // 校验传入参数
        Verify.verifyData(loginBindGoogleAuthData);

        // 正式处理业务
        Map<String, Object> data = loginService.bindGoogleAuth(loginBindGoogleAuthData, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    @PostMapping(value = "/login/googleAuth")
    @ResponseBody
    public Map<String, Object> googleAuth(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        LoginGoogleAuthData loginGoogleAuthData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<LoginGoogleAuthData>() {
        });

        // 校验传入参数
        Verify.verifyData(loginGoogleAuthData);

        // 正式处理业务
        Map<String, Object> data = loginService.googleAuth(loginGoogleAuthData, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    @GetMapping(value = "/mainframe")
    public String mainframe(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
            model.addAttribute("userType", mdmsUser.getUserType());
            model.addAttribute("userName", mdmsUser.getUserName());
            model.addAttribute("ifChangepwd", mdmsUser.getIfChangepwd());
            model.addAttribute("mdmsVer", MdmsConsts.CUR_VER);
        } catch (Exception e) {
            if (e instanceof VerifyException) {
                if (((VerifyException) e).getErrCd().equals(PltResult.RESULT_CF988.getpCode())) {
                    //session超时，直接重定向到首页
                    return "redirect:/";
                } else {
                    throw e;
                }
            } else {
                throw e;
            }
        }

        return "mainframe";
    }

}
